package sealte.hvpms.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sealte.hvpms.dao.AdminDAO;
import sealte.hvpms.dao.impl.AdminDAOImpl;
import sealte.hvpms.pojo.Admin;

public class AdminModifyPassword extends HttpServlet {

	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String op = (String)request.getParameter("oldPassword");
		String np = (String)request.getParameter("newPassword");
		Admin admin = (Admin)request.getSession().getAttribute("admin");
		PrintWriter out = response.getWriter();
		if(admin!=null){
			if(op!=null&&!op.equals("")&&np!=null&&!np.equals("")){
				String id_str = request.getParameter("id");
				int id = Integer.parseInt(id_str);
				String message_vp = "OK";
				String message_mp = "";
				AdminDAO ab = new AdminDAOImpl();
				boolean isOK = ab.validatePassword(id, op);
				if(isOK){
					boolean modifyOK = ab.modifyPassword(id, np);
					if(modifyOK){
						message_mp = "修改密码成功！";
					}else{
						message_mp = "修改密码失败！！";
					}
				}else{
					message_vp = "原密码错误！！";
				}
				request.setAttribute("message_vp", message_vp);
				request.setAttribute("message_mp", message_mp);
				request.getRequestDispatcher("hvpms_admin/modifyPassword.jsp").forward(request, response);
			}else{
				out.println("系统错误！");
			}
		}else{
			out.println("谢绝跨路径访问！");
		}
	}

}
